var arr;
function Request(_name){
	var str=location.href;
	var arr=str.split("?");
	if(arr.length==2){
		var col=arr[1].split("&");
		for(var i=0; i<col.length; i++){
			var a=col[i].split("=");
			if(a[0]==_name){
				return a[1];
			}
		}
	}
	return "";
}
onload = function(){
//--------top层账户与导航的下拉--------
	var myCount = document.getElementById("myCount");
	var degimg_0 = document.getElementById("degimg_0");
	var myCountul_0 = document.getElementById("myCountul_0");
	var netNav = document.getElementById("netNav");
	var degimg_1 = document.getElementById("degimg_1");
	var netNavul_0 = document.getElementById("netNavul_0");
	myCount.onmouseover = function(){
		degimg_0.style.transform = "rotate(180deg)";
		myCountul_0.style.display = "block";
		myCountul_0.style.border = "1px solid #ccc";
		myCountul_0.style.borderTop = "none";
	}
	myCount.onmouseout = function(){
		degimg_0.style.transform = "";
		myCountul_0.style.display = "none";
	}
	netNav.onmouseover = function(){
		degimg_1.style.transform = "rotate(180deg)";
		netNavul_0.style.display = "block";
		netNavul_0.style.border = "1px solid #ccc";
		netNavul_0.style.borderTop = "none";
	}
	netNav.onmouseout = function(){
		degimg_1.style.transform = "";
		netNavul_0.style.display = "none";
}

//-----------获取json传来的商品数据--------------
	var arrminpic = document.getElementsByClassName("minpic");
	var id=Request("id");
	var url="data/data.json?t="+new Date().getTime();
	ajax.get(url, function(str){
		var obj=eval("("+str+")");
		arr=obj.list;
		var o=getGoods(id);
		for(var i=0;i<arrminpic.length;i++){
			arrminpic[i].src =o["minsrc"+(i+1).toString()];
			var c = Number(o.count);
		}
			arrminpic[0].src = o.minsrc1;
			arrminpic[1].src = o.minsrc2;
			arrminpic[2].src = o.minsrc3;
			arrminpic[3].src = o.minsrc4;
			arrminpic[4].src = o.minsrc5;
			arrminpic[5].src = o.minsrc6;
			arrminpic[6].src = o.minsrc7;	
	});
function getGoods(id){
	for(var i=0; i<arr.length; i++){
		var o=arr[i];
		if(o.id==id){
			return o;
		}
	}
}
//--------商品分页滑动--------
	var left = document.getElementById("left_btn");
	var right = document.getElementById("right_btn");
	var mgpic_0 = document.getElementById("mgpic_0");
	var mgpic_1 = document.getElementById("mgpic_1");
	var arrli = mgpic_0.children;
	var bigpic = document.getElementById("bigpic");
	var bigerpic = document.getElementById("bigerpic");
	var glass=document.getElementsByClassName("glass")[0];
	var goodsimg = document.getElementsByClassName("goodsimg")[0];
	var goodsimg1 = document.getElementsByClassName("goodsimg1")[0];
	var now = 0;
	left.onclick = function(){
		now--;
		if(now<0){now=4}
	startMove(mgpic_0,{left:-68*now})
	}
	right.onclick = function(){
		now++;
		if(now>4){now=0}
		startMove(mgpic_0,{left:-68*now})
	}
var littletitle = document.getElementsByClassName("littletitle")[0];
var rs = document.getElementsByClassName("rs")[0];
var s469 = document.getElementsByClassName("s469")[0];
var s699 = document.getElementsByClassName("s699")[0];
var url="data/data.json?t="+new Date().getTime();
	ajax.get(url, function(str){
		var obj=eval("("+str+")");
		arr=obj.list;
		var o=getGoods(id);
	bigpic.src="images/" + o.nam + "1max.jpg";
	bigerpic.src="images/" + o.nam + "1max.jpg";
	littletitle.innerHTML = o.p;
	rs.innerHTML = o.p;
	s469.innerHTML = o.span;
	s699.innerHTML = o.span;
	document.title = o.p;
var brand = document.getElementsByClassName("brand")[0];
brand.innerHTML = o.p;
	for(var i=0;i<arrli.length;i++){
		arrli[i].index = i;
		arrli[i].onmouseover = function(){
			arrli[0].className = "";
			arrli[1].className = "";
			arrli[2].className = "";
			arrli[3].className = "";
			arrli[4].className = "";
			arrli[5].className = "";
			arrli[6].className = "";
			arrli[this.index].className="select";
			bigpic.src = "images/" + o.nam + (this.index+1)+"max.jpg";
			bigerpic.src = bigpic.src;
		}
	}
});
//------------放大镜----------------
	goodsimg.onmouseover = function(){
		goodsimg1.style.display = "block";
		glass.style.display = "block";
	}
	goodsimg.onmouseout = function(){
		goodsimg1.style.display = "none";
		glass.style.display = "none";
	}
	goodsimg.onmousemove = function(e){
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		var evt = e || event;
		var x = evt.clientX - goodsimg.offsetLeft;
		var y = evt.clientY - goodsimg.offsetTop ;
		x-=75;
		y=y - 75 +scrollTop;
		if(x<=10)x=10;
		if(x>=140)x=140;
		if(y<=10)y=10;
		if(y>=140)y=140;
		glass.style.left = x  + "px";
		glass.style.top = y  +  "px";
		bigerpic.style.left = -2*x + "px";
		bigerpic.style.top = -2*y + "px";
	}

//--------大图商品的切换--------
	var addre = document.getElementsByClassName('addre')[0];
	var adda = document.getElementsByClassName('adda')[0];
	addre.onmouseover = function(){
		addre.style.border = "1px solid #c7012c";
		adda.style.backgroundPosition = "-150px 0";
	}
	addre.onmouseout = function(){
		addre.style.border = "1px solid #ddd";
		adda.style.backgroundPosition = "-120px 0";
	}

//--------我要买 商品加减--------
	var ip_1 = document.getElementsByClassName("ip_1")[0];
	var ip_2 = document.getElementsByClassName("ip_2")[0];
	var ip_3 = document.getElementsByClassName("ip_3")[0];
	ip_1.onclick = function(){
		if(ip_2.value==1){
			alert("商品数量最少为1");
		}else{
			ip_2.value--;
		}
	}
	ip_3.onclick = function(){
			ip_2.value++;
	}

//-----------购物车-----------
	var shopping = document.getElementsByClassName("shopping")[0];
	var cargoods = document.getElementsByClassName("cargoods")[0];
	var balance = document.getElementsByClassName("balance")[0];
	var timer1 = null;
	var isin = false;
	shopping.onmouseover = function(){
		cargoods.style.display = "block";
		clearTimeout(timer1);
	}
	shopping.onmouseout = function(){
		clearTimeout(timer1);
		timer1 = setTimeout(function(){
			if(!isin){
				cargoods.style.display = "none";
			}
		},1000);
	}
	cargoods.onmouseover = function(){
		isin = true;
	}
	cargoods.onmouseout = function(){
		isin = false;
		this.style.display = "none";
	}
	balance.onclick = function(){
		window.location="cart.html";
	}

//-----------倒数计时-----------
	var thour = document.getElementsByClassName("hour")[0];
	var tminute = document.getElementsByClassName("minute")[0];
	var tsecond = document.getElementsByClassName("second")[0];
	function getRTime(){ 
	var EndTime= new Date('2017/06/30 00:00:00'); //截止时间 
	var NowTime = new Date(); 
	var t =EndTime.getTime() - NowTime.getTime();
	var d=Math.floor(t/1000/60/60/24); 
	var h1 = d*24;
	var h=Math.floor(t/1000/60/60%24)+h1; 
	var m=Math.floor(t/1000/60%60); 
	var s=Math.floor(t/1000%60); 
	if((h<10&&h>0)||h==0){
		thour.innerHTML = "0" + h;
	}else{
		thour.innerHTML = h; 
	}
	if((m<10&&m>0)||m==0){
		tminute.innerHTML = "0" + m;
	}else{
		tminute.innerHTML = m; 
	}
	if((s<10&&s>0)||s==0){
		tsecond.innerHTML = "0" + s;
	}else{
		tsecond.innerHTML = s; 
	}
} 
setInterval(getRTime,1000); 

//-------发货地址列表---------
	var changeaddre = document.getElementsByClassName("changeaddre")[0];
	var adda = document.getElementsByClassName("adda")[0];
	var addreclose = document.getElementsByClassName("addreclose")[0];
	var ressarr0 = document.getElementsByClassName("ress")[0];
	var ressarr1 = document.getElementsByClassName("ress")[1];
	var ressarr2 = document.getElementsByClassName("ress")[2];
	var ressarr3 = document.getElementsByClassName("ress")[3];
	var ressarr4 = document.getElementsByClassName("ress")[4];
	var aarr0 = ressarr0.children;
	var aarr1 = ressarr1.children;
	var aarr2 = ressarr2.children;
	var aarr3 = ressarr3.children;
	var aarr4 = ressarr4.children;
	
	var addre = document.getElementsByClassName("addre")[0];
	var innerb = addre.children[0];
	adda.onclick = function(){
		changeaddre.style.display = "block";
	}
	addreclose.onclick = function(){
		changeaddre.style.display = "none";
	}
	for(var i=0;i<aarr0.length;i++){	
		aarr0[i].index = i;
		aarr0[i].onclick = function(){
			innerb.innerHTML = aarr0[this.index].innerHTML;
			changeaddre.style.display = "none";
		}
	}
	for(var i=0;i<aarr1.length;i++){	
		aarr1[i].index = i;
		aarr1[i].onclick = function(){
			innerb.innerHTML = aarr1[this.index].innerHTML;
			changeaddre.style.display = "none";
		}
	}
	for(var i=0;i<aarr2.length;i++){	
		aarr2[i].index = i;
		aarr2[i].onclick = function(){
			innerb.innerHTML = aarr2[this.index].innerHTML;
			changeaddre.style.display = "none";
		}
	}
	for(var i=0;i<aarr3.length;i++){	
		aarr3[i].index = i;
		aarr3[i].onclick = function(){
			innerb.innerHTML = aarr3[this.index].innerHTML;
			changeaddre.style.display = "none";
		}
	}
	for(var i=0;i<aarr4.length;i++){	
		aarr4[i].index = i;
		aarr4[i].onclick = function(){
			innerb.innerHTML = aarr4[this.index].innerHTML;
			changeaddre.style.display = "none";
		}
	}

//-------产品内容选项卡切换-------
	var tab = document.getElementsByClassName("tab")[0];
	var midrightbot = document.getElementsByClassName("midrightbot")[0];
	var arrtli = tab.children;
	var arrmli = midrightbot.children;
	for(var i=0;i<arrtli.length;i++){
		arrtli[i].index = i;
		arrtli[i].onclick = function(){
			arrtli[0].className = "";
			arrtli[1].className = "";
			arrtli[2].className = "";
			arrtli[3].className = "";
			arrtli[this.index].className = "selected";
			arrmli[0].style.display = "none";
			arrmli[1].style.display = "none";
			arrmli[2].style.display = "none";
			arrmli[3].style.display = "none";
			arrmli[this.index].style.display = "block";
		}
	}

//---------------二级菜单-----------------
var classify = document.getElementsByClassName("classify")[0];
var secmenu = document.getElementsByClassName("secmenu")[0];
var secdet = document.getElementsByClassName("secdet")[0];
var arrdet = document.getElementsByClassName("secdet");
var arrsli = secmenu.children;
classify.onmouseover = function(){
	secmenu.style.display = "block";
}
secmenu.onmouseover = function(){
	secmenu.style.display = "block";
}
secdet.onmouseover = function(){
	secmenu.style.display = "block";
	secdet.style.display = "block";
}
classify.onmouseout = function(){
	secmenu.style.display = "none";
	secdet.style.display = "none";
}
secmenu.onmouseout = function(){
	secmenu.style.display = "none";
	for(var i=0;i<arrdet.length;i++){
		arrdet[i].style.display = "none";
	}
}
arrdet.onmouseout = function(){
	secmenu.style.display = "none";
	for(var i=0;i<arrdet.length;i++){
		arrdet[i].style.display = "none";
	}
}

for(var i=0;i<arrsli.length;i++){
	arrsli[i].index = i;
	arrsli[i].onmouseover = function(){
		arrsli[this.index].style.background = "#fff";
		arrsli[this.index].style.border = "2px solid #c7022d";
		arrsli[this.index].style.borderRight = "2px solid #fff";
		arrsli[this.index].style.height = "40" +"px";
		arrsli[this.index].style.Width = "227" +"px";

for(var i=0;i<arrdet.length;i++){
		arrdet[i].index = i;
		arrdet[i].onmouseover = function(){
			arrdet[this.index].style.display = "block";
			secmenu.style.display = "block";
			arrsli[this.index].style.background = "#fff";
			arrsli[this.index].style.border = "2px solid #c7022d";
			arrsli[this.index].style.borderRight = "2px solid #fff";
		}
		arrdet[i].onmouseout = function(){
			arrdet[this.index].style.display = "none";
			secmenu.style.display = "none";
			arrsli[this.index].style.border = "0px solid #c7022d";
			arrsli[this.index].style.borderBottom = "1px solid #ccc";
			arrsli[this.index].style.background = "#ffeede";
		}
	}
		for(var j=0;j<arrdet.length;j++){
			arrdet[j].style.display = "none";
		}
		arrdet[4].style.top = "80" +"px";
		arrdet[5].style.top = "126" +"px";
		arrdet[6].style.top = "171" +"px";
		arrdet[7].style.top = "215" +"px";
		arrdet[8].style.top = "261" +"px";
		arrdet[9].style.top = "305" +"px";
		arrdet[10].style.top = "349" +"px";
		arrdet[11].style.top = "393" +"px";
		arrdet[12].style.top = "437" +"px";
		arrdet[13].style.top = "455" +"px";
		arrdet[this.index].style.display = "block";
	}
	arrsli[i].onmouseout = function(){
		arrsli[this.index].style.background = "#ffeede";
		arrsli[this.index].style.borderBottom = "1px solid #ccc";
		arrsli[this.index].style.borderTop = "1px solid #ccc";
		arrsli[this.index].style.borderLeft = "1px solid #ccc";
		arrsli[this.index].style.borderRight = "1px solid #c7022d";
		
	}
}
//-------------购物车-----------------
var toaddcart = document.getElementsByClassName("addcart")[0];
toaddcart.onclick = function(){
		var o = getGoods(id);
		toaddcart.href="cart.html";
		addCart(o.id);
}
function addCart(id){//商品id
	//根据id获取cookie
	var txt=getCookie("g"+id);
	var obj, str;
	var num;
	if(txt==""){//表示该cookie不存在
		obj=getGoods(id);//根据id，得到商品对象
		num=1;		
	}else{
		obj=eval("("+txt+")");
		num=obj.num+1;		
	}
	str="{'id':'"+obj.id+"', 'p':'"+obj.p+"', 'minsrc1':'"+obj.minsrc1+"', 'span':'"+obj.span+"', 'num':"+num+"}";
	setCookie("g"+id, str, 10);
}
//------------购物车数量------------
var goodsnumb = document.getElementById("goodsnumb");
var cargoods = document.getElementsByClassName("cargoods")[0];
if(getCookie("length")!=""){
	goodsnumb.innerHTML = getCookie("length");
	cargoods.innerHTML = "您的购物车里有"+getCookie("length")+"件商品";
}else{
	goodsnumb.innerHTML = 0;
	cargoods.innerHTML = "您的购物车里还没有商品哦";
}


//---------登陆成功后用户名的修改-----------
	var welcome = document.getElementsByClassName("welcome")[0]; 
	var welto = document.getElementsByClassName("welto")[0];
	var safel = document.getElementsByClassName("safel")[0];
	var tojoin = document.getElementsByClassName("tojoin")[0];
	var fre = document.getElementsByClassName("fre")[0];
	var userlogin=getCookie("userlogin");
	if(userlogin!=""){
		welto.innerHTML = "亲爱的";
		safel.innerHTML = userlogin;
		tojoin.innerHTML = "您好";
		fre.innerHTML = "";
		var a = document.createElement("a");
		welcome.appendChild(a);
		a.innerHTML = "[退出]";
		a.onclick = function(){
			setCookie("userlogin", "", -10);
				history.go(0);
		}
	}




}